package com.example.demo.Controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.demo.Dto.ChargeDto;
import com.example.demo.Dto.TransactionInfoDto;
import com.example.demo.Service.TransactionSrevice;
import com.example.demo.VO.ChargeVo;
import com.example.demo.VO.TransactionInfoVo;
import com.example.demo.result.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/payments")
public class PayController {
    @Autowired
    private TransactionSrevice transactionSrevice;
    /**
     * 获取支付信息
     * 根据id或者preTransactionId获取支付信息
     * @param transactionInfoDto
     * @return
     */
    @GetMapping("/byId")
    public ResponseResult getPayById(@RequestBody TransactionInfoDto transactionInfoDto){
        return new ResponseResult().ok(200,"获取支付信息成功",transactionSrevice.getTransaction(transactionInfoDto));
    }
    /**
     * 获取用户所有支付信息
     * @param pageNum
     */
    @GetMapping
    public ResponseResult getAllPay(@RequestParam String pageNum, @RequestParam String pageSize){
        IPage<TransactionInfoVo> transactionList = transactionSrevice.getTransactionList(pageNum, pageSize);
        return new ResponseResult().ok(200,"获取支付信息列表成功",transactionList);
    }

    /**
     * 支付接口
     */
    @PostMapping
    public ResponseResult createPay(@RequestBody ChargeDto chargeDto){
        ChargeVo chargeVo = transactionSrevice.ChargeByOrderIdAndCreditId(chargeDto);
        return new ResponseResult().ok(200,"支付成功",chargeVo);
    }
    /**
     * 取消支付
     * @param preTransactionId
     * @return
     */
    @DeleteMapping
    public ResponseResult deletePay(@RequestParam String preTransactionId){
        transactionSrevice.deletepaymenys((preTransactionId));
        return new ResponseResult().ok(200,"取消支付成功");
    }
    @PostMapping("/confirm")
    public ResponseResult confirmPay(@RequestParam String preTransactionId){
        transactionSrevice.trueTranssation(preTransactionId);
        return new ResponseResult().ok(200,"确认支付成功");
    }
}
